package com.grablist.feihu.grablist.ui.actviity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.base.BaseActivity;
import com.grablist.feihu.grablist.bean.ClueInfo;
import com.grablist.feihu.grablist.comm.MyType;
import com.grablist.feihu.grablist.event.CloseActEvent;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.ui.actviity.user.biz.CollectionBiz;
import com.grablist.feihu.grablist.ui.actviity.user.component.DaggerCollectionComponent;
import com.grablist.feihu.grablist.ui.actviity.user.contract.CollectionContract;
import com.grablist.feihu.grablist.ui.actviity.user.module.CollectionModule;
import com.grablist.feihu.grablist.ui.actviity.user.presenter.CollectionPresenter;
import com.grablist.feihu.grablist.ui.adapter.CollectionAdapter;

/**
 * Created by admin on 2017/12/2.
 */

public class CollectionAct extends BaseActivity implements CollectionContract.View ,SuperRecyclerView.LoadingListener,  SuperBaseAdapter.OnItemClickListener{



    @Bind(R.id.super_recycler_view)
    SuperRecyclerView superRecyclerView;

    CollectionAdapter adapter;

    @Inject
    CollectionPresenter presenter;
    @Inject
    CollectionBiz biz;
    @Override
    protected void setRoot() {
        setStateText();
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_collection);

    }

    @Override
    protected void setup() {
        DaggerCollectionComponent.builder().collectionModule(new CollectionModule(this)).build().inject(this);
        setToolBar("我的收藏", "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        superRecyclerView.setLayoutManager(layoutManager);
        superRecyclerView.setRefreshEnabled(true);// 可以定制是否开启下拉刷新
        superRecyclerView.setLoadMoreEnabled(true);// 可以定制是否开启加载更多
        superRecyclerView.setLoadingListener(this);// 下拉刷新，上拉加载的监听
        superRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);// 下拉刷新的样式
        superRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);// 上拉加载的样式
        superRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);// 设置下拉箭头
        presenter.getCollection();
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }
    /***
     * Stop location service
     */
    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRemoveEvent(CloseActEvent info) {
        if (info.getType() == MyType.collectioon){
            presenter.refresh();
            presenter.getCollection();
        }
    }




    @Override
    public void onRefresh() {
        presenter.refresh();
        presenter.getCollection();

    }

    @Override
    public void onLoadMore() {
        presenter.getCollection();
    }

    @Override
    public void updateCollection(List<ClueInfo> coupons) {
        if (adapter == null) {
            adapter = new CollectionAdapter(this, coupons);
            superRecyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void refreshComplete() {
        superRecyclerView.completeRefresh();
    }

    @Override
    public void loadMoreComplete() {
        superRecyclerView.completeLoadMore();
    }

    @Override
    public void hasLoadMore(boolean b) {
        superRecyclerView.setNoMore(b);
    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
